package com.zjht.gmcc.servlet.permit;


import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;

import com.zjht.gmcc.bean.util.MyJDBC;


public class AddPermit extends BaseServlet {

	private static final long serialVersionUID = 2205937410942000119L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {
		int i_flag = 0;
		PreparedStatement ps,ps1;
		ResultSet rs;
		String s_msg="";
		String provid=request.getParameter("pid");
		String status=request.getParameter("rangtype");
		String reason=request.getParameter("reason");
		String proOrgCode=request.getParameter("porgcode");
		String permitid="";
		String sDate=request.getParameter("vDate");
		String eDate=request.getParameter("eDate");
		String dealOper=request.getParameter("dealoper");
		
		String big=request.getParameter("bigv");
		String middle=request.getParameter("miv");
		String small=request.getParameter("smv");
		String dealtype=request.getParameter("dealtype");
		String primitid=request.getParameter("permitid");
		String sql = "";	
		String str="";
		  TreeMap<String, String> b = new TreeMap<String, String>();
		  TreeMap<String, String> s = new TreeMap<String, String>();
		  TreeMap<String, String> m = new TreeMap<String, String>();
	if(dealtype.equals("0")){
		str="供应商准入信息添加成功!";
		sql = "SELECT seq_permit.nextval FROM dual";
		
		ps = myJDBC.prepareStatement(sql);

		rs = ps.executeQuery();
		if (rs.next()) {
			permitid = rs.getString(1);
			i_flag = 1;
		}
		if(i_flag == 1){
			sql="update permition set PERFLAG='0' where providerid=?"; 
			ps=myJDBC.prepareStatement(sql);
			
			ps.setString(1, provid); 
			ps.executeUpdate();
			
		}
		if(i_flag == 1){
		sql="insert into permition (permitid,startdate,enddate,status,dealoper,providerid,PERFLAG) values (?,?,?,?,?,?,1)";  
		ps=myJDBC.prepareStatement(sql);
		ps.setString(1, permitid);
		ps.setString(2, sDate);
		ps.setString(3, eDate);
		ps.setString(4, status);
		ps.setString(5, dealOper);
		ps.setString(6, provid);
		i_flag=ps.executeUpdate();
		}
		
		if(i_flag==1){
			
			for(String a:small.split(",")){
				sql="select TYPEID,PARENTTYPEID from  PRODUCTTYPE where TYPEID =(select PARENTTYPEID from  PRODUCTTYPE where TYPEID=?)";
				ps=myJDBC.prepareStatement(sql);
				ps.setString(1, a);
				rs=ps.executeQuery();
				if(rs.next()){
					s.put(a, a);
					m.put(rs.getString(1), rs.getString(1));
					b.put(rs.getString(2), rs.getString(2));
				}
				
			}
			
			for(String a:middle.split(",")){
				sql="select TYPEID from  PRODUCTTYPE where PARENTTYPEID =?";
				ps=myJDBC.prepareStatement(sql);
				ps.setString(1, a);
				rs=ps.executeQuery();
				m.put(a, a);
				while(rs.next()){
					s.put(rs.getString(1), rs.getString(1));
				}
				sql="select PARENTTYPEID from  PRODUCTTYPE where TYPEID =?";
				ps=myJDBC.prepareStatement(sql);
				ps.setString(1, a);
				rs=ps.executeQuery();
				//m.put(a, a);
				if(rs.next()){
					b.put(rs.getString(1), rs.getString(1));
				}
				
			}
			for(String a:big.split(",")){
				sql="select TYPEID,PARENTTYPEID from PRODUCTTYPE  where  PARENTTYPEID in (select TYPEID from  PRODUCTTYPE where PARENTTYPEID=? )";
				ps=myJDBC.prepareStatement(sql);
				ps.setString(1, a);
				rs=ps.executeQuery();
				b.put(a, a);
				while(rs.next()){
					s.put(rs.getString(1), rs.getString(1));
					m.put(rs.getString(2), rs.getString(2));
				}
				
				
			}
			big="";
			for(Map.Entry<String, String> en : b.entrySet()){
				if(!en.getValue().toString().equalsIgnoreCase("")){
				big+=en.getValue().toString()+","; 
				}
			} 
			middle="";
			for(Map.Entry<String, String> en : m.entrySet()){ 
				if(!en.getValue().toString().equalsIgnoreCase("")){
				middle+=en.getValue().toString()+","; 
				}
			} 
			small="";
			for(Map.Entry<String, String> en : s.entrySet()){ 
				if(!en.getValue().toString().equalsIgnoreCase("")){
				small+=en.getValue().toString()+","; 
				}
			} 
			sql="insert into permitionDetail (bigtypeid,midtypeid,smalltypeid,permitid) values (?,?,?,?)";
			ps=myJDBC.prepareStatement(sql);
			ps.setString(1, big);
			ps.setString(2, middle);
			ps.setString(3, small);
			ps.setString(4, permitid);
			i_flag=ps.executeUpdate();
		}
		
		if(i_flag == 1){
			sql="update PROVIDER set proorgcode=?,PERMITSTATUS=? where providerid=?"; 
			ps=myJDBC.prepareStatement(sql);
			ps.setString(1, proOrgCode);
			ps.setString(3, provid); 
			ps.setString(2, status);
			i_flag=ps.executeUpdate();
			
		}
		}else{
			str="供应商已经退出准入!";
			sql="update permition set enddate=? where permitid=?  ";
			ps=myJDBC.prepareStatement(sql);
			ps.setString(1, eDate);
			ps.setString(2, primitid);
			i_flag=ps.executeUpdate();
			
			if(i_flag==1){
				sql="update PROVIDER set PERMITSTATUS=? where providerid=?"; 
				ps=myJDBC.prepareStatement(sql);
				ps.setString(2, provid); 
				ps.setString(1, "0");
				i_flag=ps.executeUpdate();
			}
		}
		
		
			if (i_flag == 1) {
				alert(out, str,"permitADM.jsp");
			} else {
				errAlert(request, out, menu, s_msg);
			}
			return i_flag == 1;
		

	}

}
